<?php

namespace app\admin\controller\task;

use app\common\controller\Backend;
use think\Db;

/**
 * 订单管理
 *
 * @icon fa fa-circle-o
 */
class PaymentOrder extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\shop\Order
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'order_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\record\Money;
        $this->view->assign("payedList", $this->model->getPayedList());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index(){
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            $_where["payed"] = 2;
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->with('order')
                ->field('*,record_id as id')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     *批量催付
     *
     * 催付
     */
    public function expedit(){

        if ($this->request->isPost()) {
            $params = $this->request->post();
            if ($params)
            {
                $row = $params["row"];
                //$status = $row["status"];
                if($row["ids"]){//订单id,单个催付

                    $_where["order_id"] = $row["ids"];
                    $_where["payed"] = 2;
                    $_where["status"] = 6;
                    $order_info = Db::name("order")->where($_where)->find();

                    if(!$order_info){
                        renderJson("该订单信息不能催付！有误的话请联系技术人员",0);
                    }

                    //发送短信模板订单完成
                    $uid = $order_info["uid"];
                    $openid = Db::name("wx_users")->where("uid",$uid)->value("openid");
                    $user_name = Db::name("user")->where("uid",$uid)->value("username");
                    $time = date("Y-m-d H:i:s",time());
                    $content = [
                        $user_name,$order_info["goods_sno"],$time
                    ];//推荐内容
                    $msg = [
                        "condition"=>13,
                        "touser"=>$openid,
                        "content"=>$content
                    ];
                    $MiniMsg = new  \app\common\lib\MiniMsg($msg);
                    $MiniMsg->sendMsg();//发送短信模板订单已完成

                }

                if($params["ids"]){//批量发送



                }

            }
        }


    }





}
